package com.cedarhd.finance.log.controller.api;

import com.cedarhd.finance.log.common.JsonResult;
import com.cedarhd.finance.log.common.ProcessAPI;
import com.cedarhd.finance.log.common.enums.ResultCode;
import com.cedarhd.finance.log.controller.Routes;
import com.cedarhd.finance.log.domain.entity.ErrorLog;
import com.cedarhd.finance.log.domain.repository.specification.ErrorLogSpecification;
import com.cedarhd.finance.log.domain.service.ErrorLogService;
import com.cedarhd.finance.log.dto.ErrorLogDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2018/3/5.
 */
@RestController
@Api(value = "日志管理", description = "日志管理")
public class ErrorLogController {

    @Autowired
    private ErrorLogService errorLogService;

    private Logger logger = LoggerFactory.getLogger(ErrorLogController.class);

    @RequestMapping(value = Routes.ERRORLOG_ADD,method = RequestMethod.POST)
    @ApiOperation(value = "保存日志", notes = "保存日志", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult add(HttpServletRequest request, @RequestBody ErrorLogDto req){
        ProcessAPI api = (result, validate) -> {
            ErrorLog errorLog = errorLogService.add(req);
            result.setResult(errorLog);
        };
        return api.process(request);
    }

    @RequestMapping(value = Routes.ERRORLOG_DETAIL,method = RequestMethod.GET)
    @ApiImplicitParam(name = "errorLogId", value = "日志ID", required = true,paramType = "query",dataType = "int")
    @ApiOperation(value = "查看日志详情", notes = "查看日志详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult detail(HttpServletRequest request, Integer errorLogId){
        ProcessAPI api = (result, validate) -> {
            //检查必填参数是否为空
            validate.check("errorLogId",errorLogId);
            ErrorLog errorLog = errorLogService.getRepository().findOne(errorLogId);
            if(errorLog == null){
                result.setMsg(ResultCode.NO_DATA.getMsg());
                result.setResultCode(ResultCode.NO_DATA.getCode());
                return;
            }
            result.setResult(errorLog);
        };
        return api.process(request);
    }

    @RequestMapping(value = Routes.ERRORLOG_DELETE,method = RequestMethod.DELETE)
    @ApiImplicitParam(name = "errorLogId" , value = "日志ID", required = true,paramType = "query" , dataType = "int")
    @ApiOperation(value = "删除日志" , notes = "删除日志" , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult delete(HttpServletRequest request , Integer errorLogId){
        ProcessAPI api = (result , validate) ->{
            validate.check("errorLogId",errorLogId);
            ErrorLog errorLog = errorLogService.getRepository().findOne(errorLogId);
            if(errorLog == null){
                result.setMsg(ResultCode.NO_DATA.getMsg());
                result.setResultCode(ResultCode.NO_DATA.getCode());
                return;
            }
            errorLogService.getRepository().delete(errorLogId);
            result.setResult(errorLog);
        };
        return api.process(request);
    }

    @RequestMapping(value = Routes.ERRORLOG_LIST,method = RequestMethod.GET)
    @ApiOperation(value = "日志列表", notes = "日志列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult list(HttpServletRequest request, ErrorLogDto req){
        ProcessAPI api = (result, validate) -> {
            //动态查询数据
            Specification<ErrorLog> specification = ErrorLogSpecification.buildFromDTO(req);
            //动态查询分页数据
            Page<ErrorLog> errorLogPage = errorLogService.getRepository().findAll(specification,req.getPage());
            List<ErrorLog> errorLogList = errorLogService.coverErrorLogDTO(errorLogPage);
            result.setResult(errorLogList);
            result.setSize(errorLogPage.getTotalElements());
        };
        return api.process(request);
    }

}
